﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Setup_Menus : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindTree();

            ConfigGlobalSettings gs = ConfigGlobal.GetGlobal();
            if (gs.AllowRole)
            {
                this.allowRole.DataTextField = gs.RoleTableNameFieldName;
                this.allowRole.DataValueField = gs.RoleTablePkFieldName;
                this.allowRole.DataSource = ConfigGlobal.GetAllRoles();
                this.allowRole.DataBind();

                this.allowroleedit.DataTextField = gs.RoleTableNameFieldName;
                this.allowroleedit.DataValueField = gs.RoleTablePkFieldName;
                this.allowroleedit.DataSource = this.allowRole.DataSource;
                this.allowroleedit.DataBind();
            }
            if (gs.AllowBrowseControl)
            {
                this.allowAdmin.DataTextField = gs.UserNameColumnField;
                this.allowAdmin.DataValueField = gs.AdminPKColumnField;
                this.allowAdmin.DataSource = ConfigGlobal.GetAllAdmins();
                this.allowAdmin.DataBind();

                this.allowadminedit.DataTextField = gs.UserNameColumnField;
                this.allowadminedit.DataValueField = gs.AdminPKColumnField;
                this.allowadminedit.DataSource = this.allowAdmin.DataSource;
                this.allowadminedit.DataBind();
            }
        }
    }

    private void BindTree()
    {
        this.TreeView1.Nodes.Clear();
        //设置根节点
        TreeNode root = new TreeNode();
        root.Text = "网站";
        root.Value = "0";
        root.Expanded = true;
        this.TreeView1.Nodes.Add(root);
        ConfigMenus rootMenu = ConfigMenus.Load();
        AddMenu(root,rootMenu);
    }

    private void AddMenu(TreeNode node,ConfigMenus rootmenu)
    {        
        foreach (ConfigMenus menu in rootmenu.ChildMenus)
        {
            TreeNode newnode = new TreeNode();
            newnode.Text = menu.ShowText;
            newnode.Value = menu.ID.ToString();
                        
            node.ChildNodes.Add(newnode);            
            AddMenu(newnode, menu);
        }
    }    

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (this.TreeView1.SelectedNode == null)
        {
            Helper.Alert("选择节点");
            return;
        }
        ConfigMenus rootMenu = ConfigMenus.Load();
        ConfigMenus thisMenu=rootMenu.GetMenu(this.TreeView1.SelectedNode.Value);
        
        ConfigMenus menu = new ConfigMenus();
        if (thisMenu.ChildMenus.Count > 0)
        {
            menu.IDinChild = thisMenu.ChildMenus.Max(x => x.IDinChild) + 1;
        }        

        menu.PID = this.TreeView1.SelectedNode.Value;
        menu.ID = menu.PID + "_" + menu.IDinChild;
        menu.ShowText = this.showtext.Text;
        menu.TableName = this.tableName.Text;
        menu.TablePK = this.pkname.Text;
        menu.TableShowColumnName = this.tableshow.Text;
        menu.TableParentColumnName = this.pname.Text;
        menu.TableParentBeginValue = this.pvalue.Text;
        menu.ForgeignTableName = this.ftablename.Text;
        menu.ForgeignTableFKName = this.fkname.Text;
        menu.XmlName = this.xmlName.Text;
        menu.Url = this.url.Text;
        menu.AutoChild = this.autochild.Checked;
        menu.IsSplit = this.issplit.Checked;
        menu.FromParent = this.fp.Checked;
        menu.AllowRoles = this.allowRole.GetCheckedValues().TrimSplit();
        menu.AllowAdmins = this.allowAdmin.GetCheckedValues().TrimSplit();
        menu.UseClass = this.usemenu.Checked;
        menu.MenuClassName = this.menuclassname.Text;

        thisMenu.ChildMenus.Add(menu);
        rootMenu.Save();
        BindTree();
        
    }

    public ConfigMenus GetMenus(ConfigMenus menu, string id)
    {
        if (menu.ID == id)
        {
            return menu;
        }
        else
        {
            foreach (ConfigMenus m in menu.ChildMenus)
            {
                if (m.ID == id)
                {
                    return m;
                }
                else
                {
                    return GetMenus(m, id);
                }
            }
        }
        return null;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (this.TreeView1.SelectedNode == null)
        {
            Helper.Alert("选择节点");
            return;
        }
        ConfigMenus rootMenu = ConfigMenus.Load();
        ConfigMenus thisMenu = rootMenu.GetMenu(this.TreeView1.SelectedNode.Value);

        thisMenu.ShowText = this.showtextedit.Text;
        thisMenu.TableName = this.tableNameEdit.Text;
        thisMenu.TablePK = this.pknameedit.Text;
        thisMenu.TableShowColumnName = this.tableshowedit.Text;
        thisMenu.TableParentColumnName = this.pnameedit.Text;
        thisMenu.TableParentBeginValue = this.pvalueedit.Text;
        thisMenu.ForgeignTableName = this.ftablenameedit.Text;
        thisMenu.ForgeignTableFKName = this.fknameedit.Text;
        thisMenu.XmlName = this.xmlnameEdit.Text;
        thisMenu.Url = this.urledit.Text;
        thisMenu.AutoChild = this.autochildEdit.Checked;
        thisMenu.IsSplit = this.issplitEdit.Checked;

        thisMenu.AllowRoles = this.allowroleedit.GetCheckedValues().TrimSplit();
        thisMenu.AllowAdmins = this.allowadminedit.GetCheckedValues().TrimSplit();
        thisMenu.FromParent = this.fpedit.Checked;

        thisMenu.UseClass = this.usermenuedit.Checked;
        thisMenu.MenuClassName = this.menuclassnameedit.Text;

        rootMenu.Save();
        BindTree();
    }
    protected void delbtn_Click(object sender, EventArgs e)
    {
        if (this.TreeView1.SelectedNode == null)
        {
            Helper.Alert("选择节点");
            return;
        }
        ConfigMenus rootMenu = ConfigMenus.Load();
        ConfigMenus thisMenu = rootMenu.GetMenu(this.TreeView1.SelectedNode.Value);
        ConfigMenus father = rootMenu.GetMenu(thisMenu.PID);
        father.ChildMenus.Remove(thisMenu);
        rootMenu.Save();
        BindTree();
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        ConfigMenus rootMenu = ConfigMenus.Load();
        ConfigMenus thisMenu = rootMenu.GetMenu(this.TreeView1.SelectedNode.Value);

        this.showtextedit.Text = thisMenu.ShowText;
        this.tableNameEdit.Text = thisMenu.TableName;
        this.pknameedit.Text = thisMenu.TablePK;
        this.tableshowedit.Text = thisMenu.TableShowColumnName;
        this.pnameedit.Text = thisMenu.TableParentColumnName;
        this.pvalueedit.Text = thisMenu.TableParentBeginValue;
        this.ftablenameedit.Text = thisMenu.ForgeignTableName;
        this.fknameedit.Text = thisMenu.ForgeignTableFKName;
        this.xmlnameEdit.Text = thisMenu.XmlName;
        this.urledit.Text = thisMenu.Url;
        this.autochildEdit.Checked = thisMenu.AutoChild;
        this.issplitEdit.Checked = thisMenu.IsSplit;
        this.allowroleedit.ClearSelection();
        this.allowroleedit.CheckAll(thisMenu.AllowRoles);
        this.allowadminedit.ClearSelection();
        this.allowadminedit.CheckAll(thisMenu.AllowAdmins);
        this.fpedit.Checked = thisMenu.FromParent;
        this.usermenuedit.Checked = thisMenu.UseClass;
        this.menuclassnameedit.Text = thisMenu.MenuClassName;
    }
}
